import * as THREE from 'three'

const renderTargetOptions = {
  minFilter: THREE.LinearFilter,
  magFilter: THREE.LinearFilter,
  format: THREE.RGBAFormat,
  stencilBuffer: false,
  generateMipmaps: false
};

function createMultisampleRenderTarget(options = {}) {
  const {
    name,
    width,
    height,
    useStencilBuffer = false,
    samples = 8
  } = options;

  const renderTarget = new THREE.WebGLRenderTarget(width, height, renderTargetOptions);

  renderTarget.texture.name = `${name}.mrt`;
  renderTarget.stencilBuffer = useStencilBuffer;
  renderTarget.samples = Math.min(5, samples);
  return renderTarget;
}

export class SceneFramebuffer {
  constructor(options={}) {
    this._mrt = new createMultisampleRenderTarget(options);
  }

  get framebuffer() {
    return this._mrt;
  }

  setSize(width, height) {
    this._mrt.setSize(width, height);
  }

  dispose() {
    this._mrt.dispose();
  }
}
